#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
  int m_hSocket = 0;
  m_hSocket = socket (AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in  m_nLocalAddr;
  struct sockaddr_in  m_nPeerAddr;
  memset (&m_nLocalAddr, 0, sizeof (struct sockaddr_in));
  m_nLocalAddr.sin_family      = AF_INET;
  m_nLocalAddr.sin_addr.s_addr = INADDR_ANY;//inet_addr ("192.168.51.250");//INADDR_ANY;
  m_nLocalAddr.sin_port        = htons (9991);
  int on = 1;
  int result = setsockopt(m_hSocket, SOL_SOCKET, SO_REUSEADDR, (char *) & on, sizeof(on));
  bind (m_hSocket, (struct sockaddr*)&m_nLocalAddr, sizeof(m_nLocalAddr));
  listen (m_hSocket, 5);
  socklen_t addrLen = sizeof (struct sockaddr);
  m_hSocket = accept (m_hSocket, (struct sockaddr *)&m_nPeerAddr, &addrLen);
  printf ("accept one\n");
  char buff[2400];
  memset (buff, 0, 2400);
  int fd = open ("file", O_RDWR);
  int len = 0;
  while (1)
    {
      printf ("start to recv data\n");
      len = read (fd, buff, 2400-4);
      if (len <= 0)
	{
	  perror ("read");
	  break;
	}
      //len = htonl (len);
      struct iovec iov[2];
      iov[0].iov_base = &len;
      printf ("read len = %d\n", len);
      iov[0].iov_len  = 4;
      iov[1].iov_base = buff;
      iov[1].iov_len  = len;
      int ret = writev (m_hSocket, iov, 2);
      if (ret < 0)
	{
	  perror ("writev");
	  break;
	}
    }
  return 0;
}
